<!DOCTYPE html>
<html lang="zh_hans">
    <!-- title -->




<!-- keywords -->




<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" >
    <meta name="author" content="Apm29">
    <meta name="renderer" content="webkit">
    <meta name="copyright" content="Apm29">
    
    <meta name="keywords" content="hexo,hexo-theme,hexo-blog">
    
    <meta name="description" content="daily post">
    <meta http-equiv="Cache-control" content="no-cache">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <title>初识Ktorm · 生活中的Design</title>
    <style type="text/css">
    @font-face {
        font-family: 'Oswald-Regular';
        src: url("/font/Oswald-Regular.ttf");
    }

    body {
        margin: 0;
    }

    header,
    footer,
    .back-top,
    .sidebar,
    .container,
    .site-intro-meta,
    .toc-wrapper {
        display: none;
    }

    .site-intro {
        position: relative;
        z-index: 3;
        width: 100%;
        /* height: 50vh; */
        overflow: hidden;
    }

    .site-intro-placeholder {
        position: absolute;
        z-index: -2;
        top: 0;
        left: 0;
        width: calc(100% + 300px);
        height: 100%;
        background: repeating-linear-gradient(-45deg, #444 0, #444 80px, #333 80px, #333 160px);
        background-position: center center;
        transform: translate3d(-226px, 0, 0);
        animation: gradient-move 2.5s ease-out 0s infinite;
    }

    @keyframes gradient-move {
        0% {
            transform: translate3d(-226px, 0, 0);
        }
        100% {
            transform: translate3d(0, 0, 0);
        }
    }

</style>

    <link rel="preload" href= "/css/style.css?v=20180824" as="style" onload="this.onload=null;this.rel='stylesheet'" />
    <link rel="stylesheet" href= "/css/mobile.css?v=20180824" media="(max-width: 980px)">
    
    <link rel="preload" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.2.5/jquery.fancybox.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'" />
    
    <!-- /*! loadCSS. [c]2017 Filament Group, Inc. MIT License */
/* This file is meant as a standalone workflow for
- testing support for link[rel=preload]
- enabling async CSS loading in browsers that do not support rel=preload
- applying rel preload css once loaded, whether supported or not.
*/ -->
<script>
(function( w ){
	"use strict";
	// rel=preload support test
	if( !w.loadCSS ){
		w.loadCSS = function(){};
	}
	// define on the loadCSS obj
	var rp = loadCSS.relpreload = {};
	// rel=preload feature support test
	// runs once and returns a function for compat purposes
	rp.support = (function(){
		var ret;
		try {
			ret = w.document.createElement( "link" ).relList.supports( "preload" );
		} catch (e) {
			ret = false;
		}
		return function(){
			return ret;
		};
	})();

	// if preload isn't supported, get an asynchronous load by using a non-matching media attribute
	// then change that media back to its intended value on load
	rp.bindMediaToggle = function( link ){
		// remember existing media attr for ultimate state, or default to 'all'
		var finalMedia = link.media || "all";

		function enableStylesheet(){
			link.media = finalMedia;
		}

		// bind load handlers to enable media
		if( link.addEventListener ){
			link.addEventListener( "load", enableStylesheet );
		} else if( link.attachEvent ){
			link.attachEvent( "onload", enableStylesheet );
		}

		// Set rel and non-applicable media type to start an async request
		// note: timeout allows this to happen async to let rendering continue in IE
		setTimeout(function(){
			link.rel = "stylesheet";
			link.media = "only x";
		});
		// also enable media after 3 seconds,
		// which will catch very old browsers (android 2.x, old firefox) that don't support onload on link
		setTimeout( enableStylesheet, 3000 );
	};

	// loop through link elements in DOM
	rp.poly = function(){
		// double check this to prevent external calls from running
		if( rp.support() ){
			return;
		}
		var links = w.document.getElementsByTagName( "link" );
		for( var i = 0; i < links.length; i++ ){
			var link = links[ i ];
			// qualify links to those with rel=preload and as=style attrs
			if( link.rel === "preload" && link.getAttribute( "as" ) === "style" && !link.getAttribute( "data-loadcss" ) ){
				// prevent rerunning on link
				link.setAttribute( "data-loadcss", true );
				// bind listeners to toggle media back
				rp.bindMediaToggle( link );
			}
		}
	};

	// if unsupported, run the polyfill
	if( !rp.support() ){
		// run once at least
		rp.poly();

		// rerun poly on an interval until onload
		var run = w.setInterval( rp.poly, 500 );
		if( w.addEventListener ){
			w.addEventListener( "load", function(){
				rp.poly();
				w.clearInterval( run );
			} );
		} else if( w.attachEvent ){
			w.attachEvent( "onload", function(){
				rp.poly();
				w.clearInterval( run );
			} );
		}
	}


	// commonjs
	if( typeof exports !== "undefined" ){
		exports.loadCSS = loadCSS;
	}
	else {
		w.loadCSS = loadCSS;
	}
}( typeof global !== "undefined" ? global : this ) );
</script>

    <link rel="icon" href= "/assets/favicon.ico" />
    <link rel="preload" href="https://unpkg.com/webfontloader@1.6.28/webfontloader.js" as="script" />
    <link rel="preload" href="https://unpkg.com/jquery@3.3.1/dist/jquery.min.js" as="script" />
    <link rel="preload" href="/scripts/main.js" as="script" />
    <link rel="preload" as="font" href="/font/Oswald-Regular.ttf" crossorigin>
    <link rel="preload" as="font" href="https://at.alicdn.com/t/font_327081_1dta1rlogw17zaor.woff" crossorigin>
    
    <!-- fancybox -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.2.5/jquery.fancybox.min.js" defer></script>
    <!-- 百度统计  -->
    
    <!-- 谷歌统计  -->
    
<meta name="generator" content="Hexo 5.4.2"></head>

    
        <body class="post-body">
    
    
<header class="header">

    <div class="read-progress"></div>
    <div class="header-sidebar-menu">&#xe775;</div>
    <!-- post页的toggle banner  -->
    
    <div class="banner">
            <div class="blog-title">
                <a href="/" >生活中的Design.</a>
            </div>
            <div class="post-title">
                <a href="#" class="post-name">初识Ktorm</a>
            </div>
    </div>
    
    <a class="home-link" href=/>生活中的Design.</a>
</header>
    <div class="wrapper">
        <div class="site-intro" style="







height:50vh;
">
    
    <!-- 主页  -->
    
    
    <!-- 404页  -->
            
    <div class="site-intro-placeholder"></div>
    <div class="site-intro-img" style="background-image: url(/intro/post-bg.jpg)"></div>
    <div class="site-intro-meta">
        <!-- 标题  -->
        <h1 class="intro-title">
            <!-- 主页  -->
            
            初识Ktorm
            <!-- 404 -->
            
        </h1>
        <!-- 副标题 -->
        <p class="intro-subtitle">
            <!-- 主页副标题  -->
            
            
            <!-- 404 -->
            
        </p>
        <!-- 文章页meta -->
        
            <div class="post-intros">
                <!-- 文章页标签  -->
                
                    <div class= post-intro-tags >
    
        <a class="post-tag" href="javascript:void(0);" data-tags = "Kotlin">Kotlin</a>
    
        <a class="post-tag" href="javascript:void(0);" data-tags = "Springboot">Springboot</a>
    
        <a class="post-tag" href="javascript:void(0);" data-tags = "ORM">ORM</a>
    
</div>
                
                
                    <div class="post-intro-read">
                        <span>字数统计: <span class="post-count word-count">2.8k</span>阅读时长: <span class="post-count reading-time">14 min</span></span>
                    </div>
                
                <div class="post-intro-meta">
                    <span class="post-intro-calander iconfont-archer">&#xe676;</span>
                    <span class="post-intro-time">2021/08/09</span>
                    
                    <span id="busuanzi_container_page_pv" class="busuanzi-pv">
                        <span class="iconfont-archer">&#xe602;</span>
                        <span id="busuanzi_value_page_pv"></span>
                    </span>
                    
                    <span class="shareWrapper">
                        <span class="iconfont-archer shareIcon">&#xe71d;</span>
                        <span class="shareText">Share</span>
                        <ul class="shareList">
                            <li class="iconfont-archer share-qr" data-type="qr">&#xe75b;
                                <div class="share-qrcode"></div>
                            </li>
                            <li class="iconfont-archer" data-type="weibo">&#xe619;</li>
                            <li class="iconfont-archer" data-type="qzone">&#xe62e;</li>
                            <li class="iconfont-archer" data-type="twitter">&#xe634;</li>
                            <li class="iconfont-archer" data-type="facebook">&#xe67a;</li>
                        </ul>
                    </span>
                </div>
            </div>
        
    </div>
</div>
        <script>

  // get user agent
  var browser = {
    versions: function () {
      var u = window.navigator.userAgent;
      return {
        userAgent: u,
        trident: u.indexOf('Trident') > -1, //IE内核
        presto: u.indexOf('Presto') > -1, //opera内核
        webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
        gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
        mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
        ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
        android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
        iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者安卓QQ浏览器
        iPad: u.indexOf('iPad') > -1, //是否为iPad
        webApp: u.indexOf('Safari') == -1, //是否为web应用程序，没有头部与底部
        weixin: u.indexOf('MicroMessenger') == -1, //是否为微信浏览器
        uc: u.indexOf('UCBrowser') > -1 //是否为android下的UC浏览器
      };
    }()
  }
  console.log("userAgent:" + browser.versions.userAgent);

  // callback
  function fontLoaded() {
    console.log('font loaded');
    if (document.getElementsByClassName('site-intro-meta')) {
      document.getElementsByClassName('intro-title')[0].classList.add('intro-fade-in');
      document.getElementsByClassName('intro-subtitle')[0].classList.add('intro-fade-in');
      var postIntros = document.getElementsByClassName('post-intros')[0]
      if (postIntros) {
        postIntros.classList.add('post-fade-in');
      }
    }
  }

  // UC不支持跨域，所以直接显示
  function asyncCb() {
    if (browser.versions.uc) {
      console.log("UCBrowser");
      fontLoaded();
    } else {
      WebFont.load({
        custom: {
          families: ['Oswald-Regular']
        },
        loading: function () {  //所有字体开始加载
          // console.log('loading');
        },
        active: function () {  //所有字体已渲染
          fontLoaded();
        },
        inactive: function () { //字体预加载失败，无效字体或浏览器不支持加载
          console.log('inactive: timeout');
          fontLoaded();
        },
        timeout: 5000 // Set the timeout to two seconds
      });
    }
  }

  function asyncErr() {
    console.warn('script load from CDN failed, will load local script')
  }

  // load webfont-loader async, and add callback function
  function async(u, cb, err) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (cb) { o.addEventListener('load', function (e) { cb(null, e); }, false); }
    if (err) { o.addEventListener('error', function (e) { err(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }

  var asyncLoadWithFallBack = function (arr, success, reject) {
    var currReject = function () {
      reject()
      arr.shift()
      if (arr.length)
        async(arr[0], success, currReject)
    }

    async(arr[0], success, currReject)
  }

  asyncLoadWithFallBack([
    "https://unpkg.com/webfontloader@1.6.28/webfontloader.js",
    "https://cdn.bootcss.com/webfont/1.6.28/webfontloader.js",
    "/lib/webfontloader.min.js"
  ], asyncCb, asyncErr)
</script>
        
        <img class="loading" src="/assets/loading.svg" style="display: block; margin: 6rem auto 0 auto; width: 6rem; height: 6rem;" />
        <div class="container container-unloaded">
            <main class="main post-page">
    <article class="article-entry">
        <h1 id="初识Ktorm"><a href="#初识Ktorm" class="headerlink" title="初识Ktorm"></a>初识Ktorm</h1><h2 id="Ktorm是什么？"><a href="#Ktorm是什么？" class="headerlink" title="Ktorm是什么？"></a>Ktorm是什么？</h2><p>一个基于纯JDBC的轻量级ORM框架</p>
<ul>
<li>kotlin</li>
<li>强类型</li>
<li>SqlDSL</li>
<li>序列化API 使用 <code>filter</code>、<code>map</code>、<code>sortedBy</code> 等序列函数进行查询，就像使用 Kotlin 中的原生集合一样方便</li>
<li>无配置文件无xml，没有注解，没有其他第三方依赖</li>
<li>易扩展的设计，可以灵活编写扩展，支持更多运算符、数据类型、 SQL 函数、数据库方言等</li>
</ul>
<h2 id="Install"><a href="#Install" class="headerlink" title="Install"></a>Install</h2><p>build.gradle.kts:</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">implementation(<span class="string">&quot;org.ktorm:ktorm-core:3.4.1&quot;</span>)</span><br><span class="line"><span class="comment">//可选</span></span><br><span class="line">implementation(<span class="string">&quot;org.ktorm:ktorm-jackson:3.4.1&quot;</span>)</span><br><span class="line"><span class="comment">//可选</span></span><br><span class="line">implementation(<span class="string">&quot;org.ktorm:ktorm-support-mysql:3.4.1&quot;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="序列化"><a href="#序列化" class="headerlink" title="序列化"></a>序列化</h2><p>除了 JDK 序列化，ktorm-jackson 模块还为你提供了使用 JSON 格式进行序列化的功能。该模块为 Java 中著名的 JSON 框架 Jackson 提供了一个扩展，它支持将 Ktorm 中的实体对象格式化为 JSON，以及从 JSON 中解析实体对象。我们只需要将 <code>KtormModule</code> 注册到 <code>ObjectMapper</code> 中：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">val objectMapper = ObjectMapper()</span><br><span class="line">objectMapper.registerModule(KtormModule())</span><br></pre></td></tr></table></figure>

<h2 id="连接数据库"><a href="#连接数据库" class="headerlink" title="连接数据库"></a>连接数据库</h2><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">val</span> database = Database.connect(<span class="string">&quot;jdbc:mysql://localhost:3306/你的数据库名称&quot;</span>, user = <span class="string">&quot;数据库用户名&quot;</span>, password = <span class="string">&quot;密码&quot;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="表、实体类"><a href="#表、实体类" class="headerlink" title="表、实体类"></a>表、实体类</h2><p>描述表结构,我试着把原来项目中JPA定义的Entity类转换成Ktorm的表描述：</p>
<blockquote>
<p>原先的JPA Entity</p>
</blockquote>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Entity</span></span><br><span class="line"><span class="meta">@TableName(<span class="string">&quot;t_admin_user&quot;</span>)</span></span><br><span class="line"><span class="meta">@Table( name = <span class="string">&quot;t_admin_user&quot;</span> )</span></span><br><span class="line"><span class="keyword">data</span> <span class="keyword">class</span> <span class="title class_">AdminUser</span> <span class="meta">@JvmOverloads</span> <span class="keyword">constructor</span>(</span><br><span class="line">    <span class="meta">@TableId(type = IdType.AUTO)</span></span><br><span class="line">    <span class="meta">@Id</span></span><br><span class="line">    <span class="meta">@GeneratedValue(strategy = GenerationType.IDENTITY)</span></span><br><span class="line">    <span class="meta">@field:Positive</span>(message = <span class="string">&quot;用户ID不能为空&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;用户ID&quot;</span>)</span></span><br><span class="line">    <span class="keyword">val</span> id: <span class="built_in">Long</span>? = <span class="number">0</span>,</span><br><span class="line">    <span class="meta">@field:NotBlank</span>(message = <span class="string">&quot;用户名不能为空&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@TableField(insertStrategy = FieldStrategy.NOT_EMPTY, updateStrategy = FieldStrategy.NOT_EMPTY)</span></span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;用户名&quot;</span>)</span></span><br><span class="line">    <span class="meta">@Column( unique = true )</span></span><br><span class="line">    <span class="keyword">var</span> username: String? = <span class="literal">null</span>,</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;用户手机号&quot;</span>)</span></span><br><span class="line">    <span class="keyword">var</span> mobile: String? = <span class="string">&quot;&quot;</span>,</span><br><span class="line"></span><br><span class="line">    <span class="meta">@field:Email</span>(message = <span class="string">&quot;无效的邮箱地址&quot;</span>)</span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;用户邮箱&quot;</span>)</span></span><br><span class="line">    <span class="meta">@Column( unique = true )</span></span><br><span class="line">    <span class="keyword">var</span> mail: String? = <span class="string">&quot;&quot;</span>,</span><br><span class="line"></span><br><span class="line">    <span class="meta">@field:JsonProperty</span>(access = JsonProperty.Access.WRITE_ONLY)</span><br><span class="line">    <span class="meta">@field:NotBlank</span>(message = <span class="string">&quot;密码不能为空&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@TableField(insertStrategy = FieldStrategy.NOT_EMPTY, updateStrategy = FieldStrategy.NOT_EMPTY)</span></span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;密码&quot;</span>)</span></span><br><span class="line">    <span class="keyword">var</span> password: String? = <span class="literal">null</span>,</span><br><span class="line"></span><br><span class="line">    <span class="meta">@field:Max</span>(value = <span class="number">2</span>, message = <span class="string">&quot;错误的性别选项&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@field:Min</span>(value = <span class="number">0</span>, message = <span class="string">&quot;错误的性别选项&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@TableField(insertStrategy = FieldStrategy.NOT_EMPTY, updateStrategy = FieldStrategy.NOT_EMPTY)</span></span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;性别&quot;</span>)</span></span><br><span class="line">    <span class="keyword">var</span> gender: <span class="built_in">Int</span>? = <span class="number">0</span>,</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;生日&quot;</span>)</span></span><br><span class="line">    <span class="keyword">var</span> birthday: LocalDate? = LocalDate.ofEpochDay(<span class="number">0</span>),</span><br><span class="line"></span><br><span class="line">    <span class="meta">@TableField(<span class="string">&quot;last_login_time&quot;</span>)</span></span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;上次登陆时间&quot;</span>)</span></span><br><span class="line">    <span class="keyword">var</span> lastLoginTime: LocalDateTime? = LocalDateTime.now(),</span><br><span class="line"></span><br><span class="line">    <span class="meta">@TableField(<span class="string">&quot;last_login_ip&quot;</span>)</span></span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;上次登陆ip&quot;</span>)</span></span><br><span class="line">    <span class="keyword">var</span> lastLoginIp: String? = <span class="string">&quot;&quot;</span>,</span><br><span class="line"></span><br><span class="line">    <span class="meta">@field:Max</span>(value = <span class="number">100</span>, message = <span class="string">&quot;错误的用户等级&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@field:Min</span>(value = <span class="number">0</span>, message = <span class="string">&quot;错误的用户等级&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@TableField(<span class="string">&quot;user_level&quot;</span>)</span></span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;用户等级&quot;</span>)</span></span><br><span class="line">    <span class="keyword">var</span> userLevel: <span class="built_in">Int</span>? = <span class="number">0</span>,</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;用户昵称&quot;</span>)</span></span><br><span class="line">    <span class="keyword">var</span> nickname: String? = <span class="string">&quot;&quot;</span>,</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;用户头像&quot;</span>)</span></span><br><span class="line">    <span class="keyword">var</span> avatar: String? = <span class="string">&quot;&quot;</span>,</span><br><span class="line"></span><br><span class="line">    <span class="meta">@TableField(<span class="string">&quot;weixin_open_id&quot;</span>)</span></span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;用户微信openId&quot;</span>)</span></span><br><span class="line">    <span class="keyword">var</span> weixinOpenId: String? = <span class="string">&quot;&quot;</span>,</span><br><span class="line"></span><br><span class="line">    <span class="meta">@TableField(<span class="string">&quot;session_key&quot;</span>)</span></span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;微信登录会话KEY&quot;</span>)</span></span><br><span class="line">    <span class="keyword">var</span> sessionKey: String? = <span class="string">&quot;&quot;</span>,</span><br><span class="line"></span><br><span class="line">    <span class="meta">@field:Max</span>(value = <span class="number">2</span>, message = <span class="string">&quot;错误的用户状态&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@field:Min</span>(value = <span class="number">0</span>, message = <span class="string">&quot;错误的用户状态&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@field:Column</span>(length = <span class="number">32</span>)</span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;用户状态&quot;</span>)</span></span><br><span class="line">    <span class="keyword">var</span> status: <span class="built_in">Int</span>? = <span class="number">0</span>,</span><br><span class="line"></span><br><span class="line">    <span class="meta">@TableField(<span class="string">&quot;add_time&quot;</span>)</span></span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;用户创建时间&quot;</span>)</span></span><br><span class="line">    <span class="keyword">var</span> addTime: LocalDateTime? = LocalDateTime.now(),</span><br><span class="line"></span><br><span class="line">    <span class="meta">@TableField(<span class="string">&quot;update_time&quot;</span>)</span></span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;用户更新时间&quot;</span>)</span></span><br><span class="line">    <span class="keyword">var</span> updateTime: LocalDateTime? = LocalDateTime.now(),</span><br><span class="line"></span><br><span class="line">    <span class="meta">@TableLogic</span></span><br><span class="line">    <span class="meta">@ApiModelProperty(<span class="string">&quot;逻辑删除&quot;</span>)</span></span><br><span class="line">    <span class="meta">@field:Column</span>(length = <span class="number">1</span>)</span><br><span class="line">    <span class="keyword">var</span> deleted: <span class="built_in">Int</span>? = <span class="number">0</span>,</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Transient</span></span><br><span class="line">    <span class="keyword">var</span> roles: List&lt;Role&gt; = arrayListOf(),</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Transient</span></span><br><span class="line">    <span class="keyword">var</span> menus: List&lt;Menu&gt; = arrayListOf()</span><br><span class="line"></span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<p>先不绑定到Entity，只需<code>Table</code>的泛型设为<code>Nothing</code></p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">object</span> AdminUsers: Table&lt;<span class="built_in">Nothing</span>&gt;(<span class="string">&quot;t_admin_user&quot;</span>)&#123;</span><br><span class="line"> <span class="keyword">val</span> id = int(<span class="string">&quot;id&quot;</span>).primaryKey()</span><br><span class="line"> <span class="keyword">val</span> username = varchar(<span class="string">&quot;username&quot;</span>)</span><br><span class="line"> <span class="keyword">val</span> mobile = varchar(<span class="string">&quot;mobile&quot;</span>)</span><br><span class="line"> <span class="keyword">val</span> mail = varchar(<span class="string">&quot;mail&quot;</span>)</span><br><span class="line"> <span class="keyword">val</span> password = varchar(<span class="string">&quot;password&quot;</span>)</span><br><span class="line"> <span class="keyword">val</span> gender = int(<span class="string">&quot;gender&quot;</span>)</span><br><span class="line"> <span class="keyword">val</span> birthday = date(<span class="string">&quot;birthday&quot;</span>)</span><br><span class="line"> <span class="keyword">val</span> lastLoginTime = date(<span class="string">&quot;last_login_time&quot;</span>)</span><br><span class="line"> <span class="keyword">val</span> lastLoginIp = varchar(<span class="string">&quot;last_login_ip&quot;</span>)</span><br><span class="line"> <span class="keyword">val</span> userLevel = int(<span class="string">&quot;user_level&quot;</span>)</span><br><span class="line"> <span class="keyword">val</span> nickname = varchar(<span class="string">&quot;nickname&quot;</span>)</span><br><span class="line"> <span class="keyword">val</span> avatar = varchar(<span class="string">&quot;avatar&quot;</span>)</span><br><span class="line"> <span class="keyword">val</span> weixinOpenId = varchar(<span class="string">&quot;weixin_open_id&quot;</span>)</span><br><span class="line"> <span class="keyword">val</span> sessionKey = varchar(<span class="string">&quot;session_key&quot;</span>)</span><br><span class="line"> <span class="keyword">val</span> status = int(<span class="string">&quot;status&quot;</span>)</span><br><span class="line"> <span class="keyword">val</span> addTime = datetime(<span class="string">&quot;add_time&quot;</span>)</span><br><span class="line"> <span class="keyword">val</span> updateTime = datetime(<span class="string">&quot;update_time&quot;</span>)</span><br><span class="line"> <span class="keyword">val</span> deleted = int(<span class="string">&quot;deleted&quot;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>ktorm的表定义与实体类定义是分开的，<code>Table</code>的泛型可以指定绑定到哪个实体类，我们写个实体类，然后想办法把原来JPA Entity上Swagger、Validated相关的注解迁移到新的Ktorm实体：</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> <span class="title class_">AdminUser</span>: <span class="type">Entity</span>&lt;<span class="type">AdminUser</span>&gt;&#123;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">companion</span> <span class="keyword">object</span> : Entity.Factory&lt;AdminUser&gt;()</span><br><span class="line"></span><br><span class="line">  <span class="keyword">val</span> id: <span class="built_in">Long</span></span><br><span class="line">  <span class="keyword">var</span> username: String</span><br><span class="line">  <span class="keyword">var</span> mobile: String</span><br><span class="line">  <span class="keyword">var</span> mail: String</span><br><span class="line">  <span class="keyword">var</span> password: String</span><br><span class="line">  <span class="keyword">var</span> gender: <span class="built_in">Int</span></span><br><span class="line">  <span class="keyword">var</span> birthday: LocalDate</span><br><span class="line">  <span class="keyword">var</span> lastLoginTime: LocalDateTime</span><br><span class="line">  <span class="keyword">var</span> lastLoginIp: String</span><br><span class="line">  <span class="keyword">var</span> userLevel: <span class="built_in">Int</span></span><br><span class="line">  <span class="keyword">var</span> nickname: String</span><br><span class="line">  <span class="keyword">var</span> avatar: String</span><br><span class="line">  <span class="keyword">var</span> weixinOpenId: String</span><br><span class="line">  <span class="keyword">var</span> sessionKey: String</span><br><span class="line">  <span class="keyword">var</span> status: <span class="built_in">Int</span></span><br><span class="line">  <span class="keyword">var</span> addTime: LocalDateTime</span><br><span class="line">  <span class="keyword">var</span> updateTime: LocalDateTime</span><br><span class="line">  <span class="keyword">var</span> deleted: <span class="built_in">Int</span></span><br><span class="line">  <span class="keyword">var</span> roles: List&lt;Role&gt;</span><br><span class="line">  <span class="keyword">var</span> menus: List&lt;Menu&gt;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可以看到，Ktorm 中的实体类都继承了 <code>Entity&lt;E&gt;</code> 接口，这个接口为实体类注入了一些通用的方法。实体类的属性则使用 var 或 val 关键字直接定义即可，根据需要确定属性的类型及是否为空。有一点可能会违背你的直觉，Ktorm 中的实体类并不是 data class，甚至也不是一个普通的 class，而是 interface。这是 Ktorm 的设计要求，通过将实体类定义为 interface，Ktorm 才能够实现一些特别的功能，以后你会了解到它的意义。<br>众所周知，接口并不能被实例化，既然实体类被定义为接口，我们要如何才能创建一个实体对象呢？Ktorm 提供了一个 <code>Entity.create</code> 函数，这个函数会使用 JDK 动态代理生成实体类接口的实现，并为我们创建一个实体对象。要创建对象，可以这样写：</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">val</span> user = Entity.create&lt;AdminUser&gt;()</span><br></pre></td></tr></table></figure>

<p>因为 <code>Entity.Factory</code> 类重载了 invoke 运算符，所以你可以把这个伴随对象当函数一样直接加上括号进行调用，创建对象的代码变成了这样：</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">val</span> user = AdminUser()</span><br></pre></td></tr></table></figure>



<blockquote>
<p>但是interface上没法直接加注解，所以得定义自己的实体类（2.5版本之后可以绑定任意实体类）</p>
</blockquote>
<p>所以我们想办法把注解加到interface的field上面，既然field没有back-field无法添加注解，那我们试着加在get方法上，<code>@get:注解名称</code></p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> <span class="title class_">AdminUser</span>: <span class="type">Entity</span>&lt;<span class="type">AdminUser</span>&gt;&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">companion</span> <span class="keyword">object</span> : Entity.Factory&lt;AdminUser&gt;()</span><br><span class="line">    <span class="meta">@get:Positive</span>(message = <span class="string">&quot;用户ID不能为空&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;用户ID&quot;</span>)</span><br><span class="line">    <span class="keyword">val</span> id: <span class="built_in">Long</span></span><br><span class="line">    <span class="meta">@get:NotBlank</span>(message = <span class="string">&quot;用户名不能为空&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;用户名&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> username: String</span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;用户手机号&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> mobile: String</span><br><span class="line">    <span class="meta">@get:Email</span>(message = <span class="string">&quot;无效的邮箱地址&quot;</span>)</span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;用户邮箱&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> mail: String</span><br><span class="line">    <span class="meta">@get:JsonProperty</span>(access = JsonProperty.Access.WRITE_ONLY)</span><br><span class="line">    <span class="meta">@get:NotBlank</span>(message = <span class="string">&quot;密码不能为空&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;密码&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> password: String</span><br><span class="line">    <span class="meta">@get:Max</span>(value = <span class="number">2</span>, message = <span class="string">&quot;错误的性别选项&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@get:Min</span>(value = <span class="number">0</span>, message = <span class="string">&quot;错误的性别选项&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;性别&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> gender: <span class="built_in">Int</span></span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;生日&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> birthday: LocalDate</span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;上次登陆时间&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> lastLoginTime: LocalDateTime</span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;上次登陆ip&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> lastLoginIp: String</span><br><span class="line">    <span class="meta">@get:Max</span>(value = <span class="number">100</span>, message = <span class="string">&quot;错误的用户等级&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@get:Min</span>(value = <span class="number">0</span>, message = <span class="string">&quot;错误的用户等级&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;用户等级&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> userLevel: <span class="built_in">Int</span></span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;用户昵称&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> nickname: String</span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;用户头像&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> avatar: String</span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;用户微信openId&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> weixinOpenId: String</span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;微信登录会话KEY&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> sessionKey: String</span><br><span class="line">    <span class="meta">@get:Max</span>(value = <span class="number">2</span>, message = <span class="string">&quot;错误的用户状态&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@get:Min</span>(value = <span class="number">0</span>, message = <span class="string">&quot;错误的用户状态&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;用户状态&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> status: <span class="built_in">Int</span></span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;用户创建时间&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> addTime: LocalDateTime</span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;用户更新时间&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> updateTime: LocalDateTime</span><br><span class="line">    <span class="meta">@get:ApiModelProperty</span>(<span class="string">&quot;逻辑删除&quot;</span>)</span><br><span class="line">    <span class="keyword">var</span> deleted: <span class="built_in">Int</span></span><br><span class="line">    <span class="keyword">var</span> roles: List&lt;Role&gt;</span><br><span class="line">    <span class="keyword">var</span> menus: List&lt;Menu&gt;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<p>这样我们就可吧实体类绑定到表上了，稍微修改了下几个表字段的类型：</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">object</span> AdminUsers: Table&lt;AdminUser&gt;(<span class="string">&quot;t_admin_user&quot;</span>)&#123;</span><br><span class="line">    <span class="keyword">val</span> id = long(<span class="string">&quot;id&quot;</span>).primaryKey().bindTo &#123; it.id &#125;</span><br><span class="line">    <span class="keyword">val</span> username = varchar(<span class="string">&quot;username&quot;</span>).bindTo &#123; it.username &#125;</span><br><span class="line">    <span class="keyword">val</span> mobile = varchar(<span class="string">&quot;mobile&quot;</span>).bindTo &#123; it.mobile &#125;</span><br><span class="line">    <span class="keyword">val</span> mail = varchar(<span class="string">&quot;mail&quot;</span>).bindTo &#123; it.mail &#125;</span><br><span class="line">    <span class="keyword">val</span> password = varchar(<span class="string">&quot;password&quot;</span>).bindTo &#123; it.password &#125;</span><br><span class="line">    <span class="keyword">val</span> gender = int(<span class="string">&quot;gender&quot;</span>).bindTo &#123; it.gender &#125;</span><br><span class="line">    <span class="keyword">val</span> birthday = date(<span class="string">&quot;birthday&quot;</span>).bindTo &#123; it.birthday &#125;</span><br><span class="line">    <span class="keyword">val</span> lastLoginTime = datetime(<span class="string">&quot;last_login_time&quot;</span>).bindTo &#123; it.lastLoginTime &#125;</span><br><span class="line">    <span class="keyword">val</span> lastLoginIp = varchar(<span class="string">&quot;last_login_ip&quot;</span>).bindTo &#123; it.lastLoginIp &#125;</span><br><span class="line">    <span class="keyword">val</span> userLevel = int(<span class="string">&quot;user_level&quot;</span>).bindTo &#123; it.userLevel &#125;</span><br><span class="line">    <span class="keyword">val</span> nickname = varchar(<span class="string">&quot;nickname&quot;</span>).bindTo &#123; it.nickname &#125;</span><br><span class="line">    <span class="keyword">val</span> avatar = varchar(<span class="string">&quot;avatar&quot;</span>).bindTo &#123; it.avatar &#125;</span><br><span class="line">    <span class="keyword">val</span> weixinOpenId = varchar(<span class="string">&quot;weixin_open_id&quot;</span>).bindTo &#123; it.weixinOpenId &#125;</span><br><span class="line">    <span class="keyword">val</span> sessionKey = varchar(<span class="string">&quot;session_key&quot;</span>).bindTo &#123; it.sessionKey &#125;</span><br><span class="line">    <span class="keyword">val</span> status = int(<span class="string">&quot;status&quot;</span>).bindTo &#123; it.status &#125;</span><br><span class="line">    <span class="keyword">val</span> addTime = datetime(<span class="string">&quot;add_time&quot;</span>).bindTo &#123; it.addTime &#125;</span><br><span class="line">    <span class="keyword">val</span> updateTime = datetime(<span class="string">&quot;update_time&quot;</span>).bindTo &#123; it.updateTime &#125;</span><br><span class="line">    <span class="keyword">val</span> deleted = int(<span class="string">&quot;deleted&quot;</span>).bindTo &#123; it.deleted &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>写个简单的接口验证下：</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping(<span class="string">&quot;/user&quot;</span>)</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">UserController</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">lateinit</span> <span class="keyword">var</span> database: Database</span><br><span class="line"></span><br><span class="line">    <span class="meta">@IgnoreTokenInvalidation</span></span><br><span class="line">    <span class="meta">@PostMapping(<span class="string">&quot;/register/ktorm&quot;</span>)</span></span><br><span class="line">    <span class="meta">@ApiOperation(<span class="string">&quot;Ktorm注册用户&quot;</span>)</span></span><br><span class="line">  	<span class="meta">@Transactional</span></span><br><span class="line">    <span class="function"><span class="keyword">fun</span> <span class="title">registerWithKtorm</span><span class="params">(</span></span></span><br><span class="line"><span class="params"><span class="function">            <span class="meta">@RequestBody</span> <span class="meta">@Validated(value = [GroupAdd::class])</span> user: <span class="type">AdminUser</span></span></span></span><br><span class="line"><span class="params"><span class="function">    )</span></span>: BaseResponse&lt;AdminUser&gt; &#123;</span><br><span class="line">        database.adminUsers.add(user)</span><br><span class="line">        <span class="keyword">return</span>  BaseResponse.ok(user)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>但是实际上并不行，因为我们这个<code>AdminUser</code>是一个接口，jackson并不能把他deserialize,我们还是得新建个参数类（所以ktorm版的AdminUser上的@Validated相关接口可以去掉，移到新的参数接口上啦）：</p>
<p>参数data class:</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">data</span> <span class="keyword">class</span> <span class="title class_">JsonRegister</span> <span class="meta">@JvmOverloads</span> <span class="keyword">constructor</span>(</span><br><span class="line">        <span class="meta">@field:Positive</span>(message = <span class="string">&quot;用户ID不能为空&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>])</span><br><span class="line">        <span class="keyword">val</span> id: <span class="built_in">Long</span>? = <span class="number">0</span>,</span><br><span class="line"></span><br><span class="line">        <span class="meta">@field:NotBlank</span>(message = <span class="string">&quot;用户名不能为空&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">        <span class="keyword">var</span> username: String = <span class="string">&quot;&quot;</span>,</span><br><span class="line"></span><br><span class="line">        <span class="meta">@field:NotBlank</span>(message = <span class="string">&quot;用户邮箱不能为空&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">        <span class="keyword">var</span> mail: String = <span class="string">&quot;&quot;</span>,</span><br><span class="line"></span><br><span class="line">        <span class="meta">@field:JsonProperty</span>(access = JsonProperty.Access.WRITE_ONLY)</span><br><span class="line">        <span class="meta">@field:NotBlank</span>(message = <span class="string">&quot;密码不能为空&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">        <span class="keyword">var</span> password: String = <span class="string">&quot;&quot;</span>,</span><br><span class="line"></span><br><span class="line">        <span class="meta">@field:Max</span>(value = <span class="number">2</span>, message = <span class="string">&quot;错误的性别选项&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">        <span class="meta">@field:Min</span>(value = <span class="number">0</span>, message = <span class="string">&quot;错误的性别选项&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">        <span class="keyword">var</span> gender: <span class="built_in">Int</span>? = <span class="number">0</span>,</span><br><span class="line"></span><br><span class="line">        <span class="meta">@field:NotBlank</span>(message = <span class="string">&quot;用户Nick不能为空&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">        <span class="keyword">var</span> nickname: String = <span class="string">&quot;&quot;</span>,</span><br><span class="line">        <span class="meta">@field:NotBlank</span>(message = <span class="string">&quot;用户联系方式不能为空&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">        <span class="keyword">var</span> mobile: String = <span class="string">&quot;&quot;</span>,</span><br><span class="line">        <span class="meta">@field:NotBlank</span>(message = <span class="string">&quot;用户头像不能为空&quot;</span>, groups = [GroupEdit::<span class="keyword">class</span>, GroupAdd::<span class="keyword">class</span>])</span><br><span class="line">        <span class="keyword">var</span> avatar: String = <span class="string">&quot;&quot;</span>,</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<p>接口改一改：</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping(<span class="string">&quot;/user&quot;</span>)</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">UserController</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@PostMapping(<span class="string">&quot;/test&quot;</span>)</span></span><br><span class="line">    <span class="function"><span class="keyword">fun</span> <span class="title">test</span><span class="params">()</span></span>:BaseResponse&lt;Any&gt;&#123;</span><br><span class="line">        <span class="keyword">return</span> BaseResponse.ok(<span class="string">&quot;666&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">lateinit</span> <span class="keyword">var</span> database: Database</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">lateinit</span> <span class="keyword">var</span> tokenUtils: TokenUtils</span><br><span class="line"></span><br><span class="line">    <span class="meta">@IgnoreTokenInvalidation</span></span><br><span class="line">    <span class="meta">@PostMapping(<span class="string">&quot;/register/ktorm&quot;</span>)</span></span><br><span class="line">    <span class="meta">@ApiOperation(<span class="string">&quot;Ktorm注册用户&quot;</span>)</span></span><br><span class="line">    <span class="function"><span class="keyword">fun</span> <span class="title">registerWithKtorm</span><span class="params">(</span></span></span><br><span class="line"><span class="params"><span class="function">            <span class="meta">@RequestBody</span> <span class="meta">@Validated(value = [GroupAdd::class])</span> user: <span class="type">JsonRegister</span></span></span></span><br><span class="line"><span class="params"><span class="function">    )</span></span>: BaseResponse&lt;AdminUser&gt; &#123;</span><br><span class="line">        <span class="keyword">val</span> adminUser = AdminUser &#123;</span><br><span class="line">            username = user.username</span><br><span class="line">            mail = user.mail</span><br><span class="line">            password = user.password</span><br><span class="line">            gender = user.gender</span><br><span class="line">            nickname = user.nickname</span><br><span class="line">            mobile = user.mobile</span><br><span class="line">            avatar = user.avatar</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">val</span> insert = <span class="keyword">try</span> &#123;</span><br><span class="line">            database.adminUsers.add(adminUser)</span><br><span class="line">        &#125; <span class="keyword">catch</span> (e: Exception) &#123;</span><br><span class="line">            <span class="keyword">when</span> (e.cause) &#123;</span><br><span class="line">                <span class="keyword">is</span> SQLIntegrityConstraintViolationException -&gt; <span class="keyword">throw</span> ServiceException(<span class="number">500</span>, <span class="string">&quot;账号已存在&quot;</span>)</span><br><span class="line">                <span class="keyword">else</span> -&gt; <span class="keyword">throw</span> ServiceException(<span class="number">500</span>, <span class="string">&quot;未知错误,注册用户失败&quot;</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">val</span> databaseUser =  <span class="keyword">if</span> (insert &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            database.adminUsers.find &#123;</span><br><span class="line">                it.username eq adminUser.username</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="literal">null</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span>  BaseResponse.depends(</span><br><span class="line">                databaseUser!=<span class="literal">null</span>,</span><br><span class="line">                errMsg = <span class="string">&quot;注册失败&quot;</span>,</span><br><span class="line">                successMsg = <span class="string">&quot;注册成功&quot;</span>,</span><br><span class="line">                token = tokenUtils.createTokenWithKtormUser(databaseUser),</span><br><span class="line">                <span class="keyword">data</span> = databaseUser</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>测试下参数校验：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;code&quot;</span><span class="punctuation">:</span> <span class="number">400</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;msg&quot;</span><span class="punctuation">:</span> <span class="string">&quot;参数验证失败(mobile):用户联系方式不能为空&quot;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;data&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">null</span></span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;token&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">null</span></span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<p>再试着填完参数测试：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;code&quot;</span><span class="punctuation">:</span> <span class="number">200</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;msg&quot;</span><span class="punctuation">:</span> <span class="string">&quot;注册成功&quot;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;data&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;id&quot;</span><span class="punctuation">:</span> <span class="number">23</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;username&quot;</span><span class="punctuation">:</span> <span class="string">&quot;admin-test&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;mobile&quot;</span><span class="punctuation">:</span> <span class="string">&quot;17376508275&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;mail&quot;</span><span class="punctuation">:</span> <span class="string">&quot;835170486@qq.com&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;gender&quot;</span><span class="punctuation">:</span> <span class="number">0</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;nickname&quot;</span><span class="punctuation">:</span> <span class="string">&quot;测试管理员&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;avatar&quot;</span><span class="punctuation">:</span> <span class="string">&quot;https://www.ktorm.org/images/logo-middle.png&quot;</span></span><br><span class="line">  <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>


    </article>
    <!-- license  -->
    
        <div class="license-wrapper">
            <p>原文作者：<a href="https://apm29.github.io">Apm29</a>
            <p>原文链接：<a href="https://apm29.github.io/2021/08/09/%E5%88%9D%E8%AF%86Ktorm/">https://apm29.github.io/2021/08/09/%E5%88%9D%E8%AF%86Ktorm/</a>
            <p>发表日期：<a href="https://apm29.github.io/2021/08/09/%E5%88%9D%E8%AF%86Ktorm/">August 9th 2021, 4:27:06 pm</a>
            <p>更新日期：<a href="https://apm29.github.io/2021/08/09/%E5%88%9D%E8%AF%86Ktorm/">August 11th 2021, 3:28:34 pm</a>
            <p>版权声明：本文采用<a rel="license noopener" target="_blank" href="http://creativecommons.org/licenses/by-nc/4.0/">知识共享署名-非商业性使用 4.0 国际许可协议</a>进行许可</p>
        </div>
    
    <!-- paginator  -->
    <ul class="post-paginator">
        <li class="next">
            
                <div class="nextSlogan">Next Post</div>
                <a href= "/2021/12/21/Kotlin%E5%8D%8F%E7%A8%8B%E7%9B%B8%E5%85%B3/" title= "Kotlin协程相关">
                    <div class="nextTitle">Kotlin协程相关</div>
                </a>
            
        </li>
        <li class="previous">
            
                <div class="prevSlogan">Previous Post</div>
                <a href= "/2021/08/05/NUXTJS%E7%9A%84%E5%A4%9A%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE/" title= "NUXTJS的多环境配置">
                    <div class="prevTitle">NUXTJS的多环境配置</div>
                </a>
            
        </li>
    </ul>
    <!-- 评论插件 -->
    <!-- 来必力City版安装代码 -->

<!-- City版安装代码已完成 -->
    
    
    <!-- partial('_partial/comment/changyan') -->
    <!--PC版-->


    
    

    <!-- 评论 -->
</main>
            <!-- profile -->
            
        </div>
        <footer class="footer footer-unloaded">
    <!-- social  -->
    
    <div class="social">
        
    
        
            
                <a href="mailto:yjw999wow@gmail.com" class="iconfont-archer email" title=email ></a>
            
        
    
        
            
                <a href="//github.com/apm29" class="iconfont-archer github" target="_blank" title=github></a>
            
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    

    </div>
    
    <!-- powered by Hexo  -->
    <div class="copyright">
        <span id="hexo-power">Powered by <a href="https://hexo.io/" target="_blank">Hexo</a></span><span class="iconfont-archer power">&#xe635;</span><span id="theme-info">theme <a href="https://github.com/fi3ework/hexo-theme-archer" target="_blank">Archer</a></span>
    </div>
    <div style="text-align: center">
        <img src="">
        <a href="https://beian.miit.gov.cn/"  style="color:#f72b07" target="_blank">浙ICP备20011000号-1</a>
    </div>
    <!-- 不蒜子  -->
    
    <div class="busuanzi-container">
    
    
    <span id="busuanzi_container_site_pv">PV: <span id="busuanzi_value_site_pv"></span> :)</span>
    
    </div>
    
</footer>

    </div>
    <!-- toc -->
    
    <div class="toc-wrapper" style=
    







top:50vh;

    >
        <div class="toc-catalog">
            <span class="iconfont-archer catalog-icon">&#xe613;</span><span>CATALOG</span>
        </div>
        <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%88%9D%E8%AF%86Ktorm"><span class="toc-number">1.</span> <span class="toc-text">初识Ktorm</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ktorm%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="toc-number">1.1.</span> <span class="toc-text">Ktorm是什么？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Install"><span class="toc-number">1.2.</span> <span class="toc-text">Install</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BA%8F%E5%88%97%E5%8C%96"><span class="toc-number">1.3.</span> <span class="toc-text">序列化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-number">1.4.</span> <span class="toc-text">连接数据库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%A1%A8%E3%80%81%E5%AE%9E%E4%BD%93%E7%B1%BB"><span class="toc-number">1.5.</span> <span class="toc-text">表、实体类</span></a></li></ol></li></ol>
    </div>
    
    <div class="back-top iconfont-archer">&#xe639;</div>
    <div class="sidebar sidebar-hide">
    <ul class="sidebar-tabs sidebar-tabs-active-0">
        <li class="sidebar-tab-archives"><span class="iconfont-archer">&#xe67d;</span><span class="tab-name">Archive</span></li>
        <li class="sidebar-tab-tags"><span class="iconfont-archer">&#xe61b;</span><span class="tab-name">Tag</span></li>
        <li class="sidebar-tab-categories"><span class="iconfont-archer">&#xe666;</span><span class="tab-name">Cate</span></li>
    </ul>
    <div class="sidebar-content sidebar-content-show-archive">
          <div class="sidebar-panel-archives">
    <!-- 在ejs中将archive按照时间排序 -->
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    <div class="total-and-search">
        <div class="total-archive">
        Total : 44
        </div>
        <!-- search  -->
        
    </div>
    
    <div class="post-archive">
    
    
    
    
    <div class="archive-year"> 2022 </div>
    <ul class="year-list">
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">07/06</span><a class="archive-post-title" href= "/2022/07/06/%E4%BD%BF%E7%94%A8KeepAlive%E5%8A%A8%E6%80%81%E7%BC%93%E5%AD%98%E6%A0%87%E7%AD%BE%E9%A1%B5/" >使用KeepAlive动态缓存标签页</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">07/05</span><a class="archive-post-title" href= "/2022/07/05/SheetJS%20--%20%E5%89%8D%E7%AB%AF(%E6%B5%8F%E8%A7%88%E5%99%A8)%E5%AF%BC%E5%87%BAExcel%E6%96%87%E4%BB%B6/" >SheetJS -- 前端(浏览器)导出Excel文件</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">06/22</span><a class="archive-post-title" href= "/2022/06/22/%E9%92%89%E9%92%89H5%E5%BA%94%E7%94%A8%E8%B8%A9%E5%9D%91(1)/" >钉钉H5应用踩坑(1)</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">06/20</span><a class="archive-post-title" href= "/2022/06/20/%E4%BD%BF%E7%94%A8-vue-composition-api/" >使用@vue/composition-api</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">06/06</span><a class="archive-post-title" href= "/2022/06/06/Python%E6%B8%85%E9%99%A4NodeModules%E6%96%87%E4%BB%B6%E5%A4%B9%E8%84%9A%E6%9C%AC/" >Python清除NodeModules文件夹脚本</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">06/06</span><a class="archive-post-title" href= "/2022/06/06/%E5%AF%B9%E6%9C%80%E8%BF%91%E5%86%99%E7%9A%84Android%E5%BA%94%E7%94%A8%E7%9A%84%E6%80%BB%E7%BB%93(1)/" >对最近写的Android应用的总结(1)</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">05/31</span><a class="archive-post-title" href= "/2022/05/31/Vite-Plugin-Pages%E4%B8%8EKeep-Alive/" >Vite-Plugin-Pages与Keep-Alive</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">05/27</span><a class="archive-post-title" href= "/2022/05/27/VSCode%E9%85%8D%E7%BD%AE%E4%B8%8E%E6%8F%92%E4%BB%B6(Vue%E5%BC%80%E5%8F%91)/" >VSCode配置与插件（Vue开发）</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">01/19</span><a class="archive-post-title" href= "/2022/01/19/%E9%9D%99%E6%80%81%E7%BD%91%E7%AB%99%E7%94%9F%E6%88%90%E5%99%A8-Gatsby-%E5%AD%A6%E4%B9%A0/" >静态网站生成器 Gatsby 学习</a>
        </li>
    
    
    
    
    
        </ul>
    
    <div class="archive-year"> 2021 </div>
    <ul class="year-list">
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/21</span><a class="archive-post-title" href= "/2021/12/21/Kotlin%E5%8D%8F%E7%A8%8B%E7%9B%B8%E5%85%B3/" >Kotlin协程相关</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">08/09</span><a class="archive-post-title" href= "/2021/08/09/%E5%88%9D%E8%AF%86Ktorm/" >初识Ktorm</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">08/05</span><a class="archive-post-title" href= "/2021/08/05/NUXTJS%E7%9A%84%E5%A4%9A%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE/" >NUXTJS的多环境配置</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">08/05</span><a class="archive-post-title" href= "/2021/08/05/NuxtJS%E9%9D%9E%E6%A0%B9%E7%9B%AE%E5%BD%95%E4%B8%8B%E9%83%A8%E7%BD%B2/" >NuxtJS非根目录下部署</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">07/24</span><a class="archive-post-title" href= "/2021/07/24/%E4%BD%BF%E7%94%A8vue-sfc-rollup%E5%88%9B%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84NPM%E5%BA%93/" >使用vue-sfc-rollup创建自己的NPM库</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">07/19</span><a class="archive-post-title" href= "/2021/07/19/Certbot%E7%94%9F%E6%88%90Https%E8%AF%81%E4%B9%A6/" >Certbot生成Https证书</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">07/19</span><a class="archive-post-title" href= "/2021/07/19/Ubuntu%E4%B8%ADMysql5-0%E9%87%8D%E6%96%B0%E5%AE%89%E8%A3%85%E4%B8%8E%E5%88%9D%E5%A7%8B%E5%8C%96/" >Ubuntu中Mysql5.0重新安装与初始化</a>
        </li>
    
    
    
    
    
        </ul>
    
    <div class="archive-year"> 2020 </div>
    <ul class="year-list">
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">10/28</span><a class="archive-post-title" href= "/2020/10/28/JetPack-Navigation%E4%B8%ADFragment%E7%9A%84%E8%BF%94%E5%9B%9E%E9%94%AE%E6%8B%A6%E6%88%AA/" >JetPack-Navigation中Fragment的返回键拦截</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">07/07</span><a class="archive-post-title" href= "/2020/07/07/Flutter%E5%9B%BD%E9%99%85%E5%8C%96flutter-intl/" >Flutter国际化flutter_intl</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">06/18</span><a class="archive-post-title" href= "/2020/06/18/Vue%E9%A1%B9%E7%9B%AE%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1%E5%AE%9E%E8%B7%B5%EF%BC%88%E5%AE%9E%E7%8E%B0%E5%8A%A8%E6%80%81%E6%9D%83%E9%99%90%EF%BC%89/" >Vue项目动态路由实践（实现动态权限）</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">05/29</span><a class="archive-post-title" href= "/2020/05/29/%E7%94%A8%E5%88%86%E6%B2%BB%E6%80%9D%E6%83%B3%E9%9D%9E%E9%80%92%E5%BD%92%E8%8E%B7%E5%8F%96N%E4%B8%AA%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E7%9A%84%E7%BB%84%E5%90%88/" >用分治思想非递归获取N个数组元素的组合</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">03/23</span><a class="archive-post-title" href= "/2020/03/23/TravisCi-Gradle%E9%83%A8%E7%BD%B2SpringBoot%E9%A1%B9%E7%9B%AE%E5%88%B0aliyun-ECS/" >TravisCi/Gradle部署SpringBoot项目到aliyun ECS</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">01/07</span><a class="archive-post-title" href= "/2020/01/07/Linux-OSX-%E5%91%BD%E4%BB%A4/" >Linux/OSX 命令</a>
        </li>
    
    
    
    
    
        </ul>
    
    <div class="archive-year"> 2019 </div>
    <ul class="year-list">
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/23</span><a class="archive-post-title" href= "/2019/12/23/%E7%94%A8Flutter%E9%87%8D%E6%9E%84%E5%BA%94%E7%94%A8/" >用Flutter重构应用</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/20</span><a class="archive-post-title" href= "/2019/12/20/Docker%E7%AC%94%E8%AE%B0/" >Docker笔记</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/19</span><a class="archive-post-title" href= "/2019/12/19/Hexo/" >Hexo</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/19</span><a class="archive-post-title" href= "/2019/12/19/Android-Text-measure-draw/" >Android Text measure/draw</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/19</span><a class="archive-post-title" href= "/2019/12/19/Flutter-WebView-%E9%94%AE%E7%9B%98%E9%97%AE%E9%A2%98/" >Flutter WebView 键盘问题</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/19</span><a class="archive-post-title" href= "/2019/12/19/Git-Submodule-%E5%AD%90%E6%A8%A1%E5%9D%97/" >Git Submodule(子模块)</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/18</span><a class="archive-post-title" href= "/2019/12/18/Android-Webview%E8%AE%BE%E7%BD%AECookie/" >Android Webview设置Cookie</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/18</span><a class="archive-post-title" href= "/2019/12/18/NPM%E8%AE%BE%E7%BD%AERegistry/" >NPM设置Registry</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/17</span><a class="archive-post-title" href= "/2019/12/17/LiveData/" >LiveData</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/13</span><a class="archive-post-title" href= "/2019/12/13/AndroidDevMetrics/" >AndroidDevMetrics</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/11</span><a class="archive-post-title" href= "/2019/12/11/CustomPainter/" >CustomPaint</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/11</span><a class="archive-post-title" href= "/2019/12/11/Dart%E5%AD%A6%E4%B9%A0/" >Dart 语法</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/11</span><a class="archive-post-title" href= "/2019/12/11/Databinding%20in%20kotlin/" >DataBinding in kotlin</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/11</span><a class="archive-post-title" href= "/2019/12/11/Gradle%E5%AD%A6%E4%B9%A0/" >Gradle与Groovy</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/11</span><a class="archive-post-title" href= "/2019/12/11/JetpackSetup/" >Jetpack</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/11</span><a class="archive-post-title" href= "/2019/12/11/PDFViewerInAndroid/" >PDFViewer on Android</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/11</span><a class="archive-post-title" href= "/2019/12/11/Navigation/" >Navigation初探</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/11</span><a class="archive-post-title" href= "/2019/12/11/RxCache/" >RxCache</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/11</span><a class="archive-post-title" href= "/2019/12/11/RxLifecycle%E5%AD%A6%E4%B9%A0/" >RxLifecycle</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/11</span><a class="archive-post-title" href= "/2019/12/11/Spring/" >SpringFrameWork</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/11</span><a class="archive-post-title" href= "/2019/12/11/TextView%E4%B8%AD%E6%98%BE%E7%A4%BAHtml%E5%86%85%E5%AE%B9/" >在TextView中显示Html格式的文字</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/11</span><a class="archive-post-title" href= "/2019/12/11/flutter/" >Flutter</a>
        </li>
    
    </div>
  </div>
        <div class="sidebar-panel-tags">
    <div class="sidebar-tags-name">
    
        <span class="sidebar-tag-name" data-tags="Android"><span class="iconfont-archer">&#xe606;</span>Android</span>
    
        <span class="sidebar-tag-name" data-tags="JetPack"><span class="iconfont-archer">&#xe606;</span>JetPack</span>
    
        <span class="sidebar-tag-name" data-tags="Tools"><span class="iconfont-archer">&#xe606;</span>Tools</span>
    
        <span class="sidebar-tag-name" data-tags="Linux"><span class="iconfont-archer">&#xe606;</span>Linux</span>
    
        <span class="sidebar-tag-name" data-tags="Flutter"><span class="iconfont-archer">&#xe606;</span>Flutter</span>
    
        <span class="sidebar-tag-name" data-tags="Dart"><span class="iconfont-archer">&#xe606;</span>Dart</span>
    
        <span class="sidebar-tag-name" data-tags="Kotlin"><span class="iconfont-archer">&#xe606;</span>Kotlin</span>
    
        <span class="sidebar-tag-name" data-tags="DataBinding"><span class="iconfont-archer">&#xe606;</span>DataBinding</span>
    
        <span class="sidebar-tag-name" data-tags="Docker"><span class="iconfont-archer">&#xe606;</span>Docker</span>
    
        <span class="sidebar-tag-name" data-tags="Git"><span class="iconfont-archer">&#xe606;</span>Git</span>
    
        <span class="sidebar-tag-name" data-tags="Gradle"><span class="iconfont-archer">&#xe606;</span>Gradle</span>
    
        <span class="sidebar-tag-name" data-tags="Groovy"><span class="iconfont-archer">&#xe606;</span>Groovy</span>
    
        <span class="sidebar-tag-name" data-tags="NPM"><span class="iconfont-archer">&#xe606;</span>NPM</span>
    
        <span class="sidebar-tag-name" data-tags="travisci"><span class="iconfont-archer">&#xe606;</span>travisci</span>
    
        <span class="sidebar-tag-name" data-tags="Fragment"><span class="iconfont-archer">&#xe606;</span>Fragment</span>
    
        <span class="sidebar-tag-name" data-tags="Coroutines"><span class="iconfont-archer">&#xe606;</span>Coroutines</span>
    
        <span class="sidebar-tag-name" data-tags="Shell"><span class="iconfont-archer">&#xe606;</span>Shell</span>
    
        <span class="sidebar-tag-name" data-tags="前端"><span class="iconfont-archer">&#xe606;</span>前端</span>
    
        <span class="sidebar-tag-name" data-tags="nuxtjs"><span class="iconfont-archer">&#xe606;</span>nuxtjs</span>
    
        <span class="sidebar-tag-name" data-tags="Python"><span class="iconfont-archer">&#xe606;</span>Python</span>
    
        <span class="sidebar-tag-name" data-tags="SheetJS"><span class="iconfont-archer">&#xe606;</span>SheetJS</span>
    
        <span class="sidebar-tag-name" data-tags="Spring"><span class="iconfont-archer">&#xe606;</span>Spring</span>
    
        <span class="sidebar-tag-name" data-tags="Springboot"><span class="iconfont-archer">&#xe606;</span>Springboot</span>
    
        <span class="sidebar-tag-name" data-tags="TravisCI"><span class="iconfont-archer">&#xe606;</span>TravisCI</span>
    
        <span class="sidebar-tag-name" data-tags="MySQL"><span class="iconfont-archer">&#xe606;</span>MySQL</span>
    
        <span class="sidebar-tag-name" data-tags="Vue"><span class="iconfont-archer">&#xe606;</span>Vue</span>
    
        <span class="sidebar-tag-name" data-tags="Vite"><span class="iconfont-archer">&#xe606;</span>Vite</span>
    
        <span class="sidebar-tag-name" data-tags="ORM"><span class="iconfont-archer">&#xe606;</span>ORM</span>
    
        <span class="sidebar-tag-name" data-tags="算法"><span class="iconfont-archer">&#xe606;</span>算法</span>
    
        <span class="sidebar-tag-name" data-tags="javascript"><span class="iconfont-archer">&#xe606;</span>javascript</span>
    
        <span class="sidebar-tag-name" data-tags="SSG"><span class="iconfont-archer">&#xe606;</span>SSG</span>
    
        <span class="sidebar-tag-name" data-tags="SEO"><span class="iconfont-archer">&#xe606;</span>SEO</span>
    
    </div>
    <div class="iconfont-archer sidebar-tags-empty">&#xe678;</div>
    <div class="tag-load-fail" style="display: none; color: #ccc; font-size: 0.6rem;">
    缺失模块。<br/>
    1、请确保node版本大于6.2<br/>
    2、在博客根目录（注意不是archer根目录）执行以下命令：<br/>
    <span style="color: #f75357; font-size: 1rem; line-height: 2rem;">npm i hexo-generator-json-content --save</span><br/>
    3、在根目录_config.yml里添加配置：
    <pre style="color: #787878; font-size: 0.6rem;">
jsonContent:
  meta: false
  pages: false
  posts:
    title: true
    date: true
    path: true
    text: false
    raw: false
    content: false
    slug: false
    updated: false
    comments: false
    link: false
    permalink: false
    excerpt: false
    categories: true
    tags: true</pre>
    </div> 
    <div class="sidebar-tags-list"></div>
</div>
        <div class="sidebar-panel-categories">
    <div class="sidebar-categories-name">
    
        <span class="sidebar-category-name" data-categories="笔记"><span class="iconfont-archer">&#xe60a;</span>笔记</span>
    
        <span class="sidebar-category-name" data-categories="Git"><span class="iconfont-archer">&#xe60a;</span>Git</span>
    
    </div>
    <div class="iconfont-archer sidebar-categories-empty">&#xe678;</div>
    <div class="sidebar-categories-list"></div>
</div>
    </div>
</div> 
    <script>
    var siteMeta = {
        root: "/",
        author: "Apm29"
    }
</script>
    <!-- CDN failover -->
    <script src="https://unpkg.com/jquery@3.3.1/dist/jquery.min.js"></script>
    <script type="text/javascript">
        if (typeof window.$ === 'undefined')
        {
            console.warn('jquery load from jsdelivr failed, will load local script')
            document.write('<script src="/lib/jquery.min.js">\x3C/script>')
        }
    </script>
    <script src="/scripts/main.js"></script>
    <!-- algolia -->
    
    <!-- busuanzi  -->
    
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    
    <!-- CNZZ  -->
    
    </div>
    <!-- async load share.js -->
    
        <script src="/scripts/share.js" async></script>    
     
    </body>
</html>


